<template>
  <a-card class="card-wrapper" :loading="loading" title="超期工单">
    <a-table size="small" :columns="columns" :data-source="tableData" :pagination="false" :loading="loading" :scroll="{ y: '100%' }" row-key="id">
      <template #bodyCell="{ text, record, index, column }">
        <template v-if="column.dataIndex === 'code'">
          <template v-if="record.clickFlag">{{ text }}</template>
          <a-button v-else @click="toDetail(record)" type="link">{{ text }}</a-button>
        </template>
      </template>
    </a-table>
    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        show-less-items
        :pageSizeOptions="[4, ...PAGE_SIZE_OPTIONS]"
        :defaultPageSize="params.pageSize"
        v-model:current="params.pageNum"
        v-model:pageSize="params.pageSize"
        :total="total"
        @change="fetchData"
        @showSizeChange="fetchData"
        :show-total="showTableTotal"
      />
    </div>
  </a-card>
</template>
<script setup>
  import { ref, onMounted, reactive } from 'vue';
  import { smartSentry } from '/@/lib/smart-sentry';
  import { PAGE_SIZE_OPTIONS, showTableTotal } from '/@/constants/common-const';
  import { useRouter } from 'vue-router';

  const router = useRouter();
  const loading = ref(true);
  const total = ref(0);
  const tableData = ref([]);
  const params = reactive({
    pageNum: 1,
    pageSize: 4,
  });
  const columns = ref([
    {
      title: '背调订单号码',
      dataIndex: 'code',
      width: 160,
    },
    {
      title: '候选人姓名',
      dataIndex: 'name',
      ellipsis: true,
      width: 100,
    },
    {
      title: '候选人证件号码',
      dataIndex: 'idCard',
      ellipsis: true,
    },
    {
      title: '组织',
      dataIndex: 'departmentName',
      ellipsis: true,
    },
    {
      title: '处理人',
      dataIndex: 'handlerName',
      ellipsis: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      ellipsis: true,
    },
  ]);

  const toDetail = (record) => {
    router.push({
      path: '/hrssc/ticket/ticket-detail',
      query: {
        ticketTaskId: record.taskId,
        id: record.id,
        backPath: '/home',
      },
    });
  };

  // const item = {
  //   code: 'TD20230118NO00002',
  //   createTime: '2023-01-18 02:22:43',
  //   departmentName: '松花湖',
  //   handler: 84,
  //   handlerName: 'exceptionUser',
  //   id: 30,
  //   idCard: '210211198503041234',
  //   name: '张三',
  //   taskId: 102,
  //   ticketId: null,
  // };

  const fetchData = async () => {
    try {
      loading.value = true;
      const res = {
        "code": 0,
        "level": null,
        "msg": "success",
        "ok": true,
        "data": {
          "pageNum": 1,
          "pageSize": 4,
          "total": 117,
          "pages": 30,
          "list": [
            {
              "id": 46,
              "ticketId": 31,
              "taskId": 51,
              "code": "TD20230226NO00003",
              "name": "刘佳阳",
              "idCard": "222406198307010617",
              "departmentName": null,
              "handlerName": "刘华田",
              "handler": 1,
              "createTime": "2023-02-26 18:52:02",
              "now": "2025-09-29 14:26:51",
              "days": 1,
              "clickFlag": false,
              "loginName": "admin"
            },
            {
              "id": 48,
              "ticketId": 33,
              "taskId": 61,
              "code": "TD20230226NO00005",
              "name": "刘佳阳",
              "idCard": "222406198307010617",
              "departmentName": null,
              "handlerName": "肖玉",
              "handler": 91,
              "createTime": "2023-02-26 20:26:01",
              "now": "2025-09-29 14:26:51",
              "days": 1,
              "clickFlag": false,
              "loginName": "xiaoy101"
            },
            {
              "id": 49,
              "ticketId": 34,
              "taskId": 67,
              "code": "TD20230226NO00006",
              "name": "刘佳阳",
              "idCard": "222406198307010617",
              "departmentName": null,
              "handlerName": "肖玉",
              "handler": 91,
              "createTime": "2023-02-26 20:26:02",
              "now": "2025-09-29 14:26:51",
              "days": 1,
              "clickFlag": false,
              "loginName": "xiaoy101"
            },
            {
              "id": 50,
              "ticketId": 35,
              "taskId": 74,
              "code": "TD20230226NO00007",
              "name": "刘佳阳",
              "idCard": "222406198307010617",
              "departmentName": null,
              "handlerName": "肖玉",
              "handler": 91,
              "createTime": "2023-02-26 21:36:02",
              "now": "2025-09-29 14:26:51",
              "days": 1,
              "clickFlag": false,
              "loginName": "xiaoy101"
            }
          ],
          "emptyFlag": null
        }
      }
      if (res.code === 0) {
        tableData.value = res.data.list || [];
        total.value = res.data.total || 0;
      }
      // tableData.value = new Array(4).fill(item);
      // total.value = 4;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      loading.value = false;
    }
  };

  onMounted(() => {
    fetchData();
  });
</script>
<style lang="less" scoped>
  .card-wrapper {
    :deep(.ant-card-body) {
      padding: 1px 0 4px 0;
      .ant-btn-link {
        padding: 0;
        height: 20px;
      }
    }
  }
</style>
